# -*- coding: utf-8 -*-

import pydot
from sets import Set

graph = pydot.Dot(graph_type='digraph')

f = open("/<WORKSPACE>/tp1/experimentos/msg_big")

nodos = {}
agregados = Set()
count = 0
maxCount = 5000;

def newNode(addr):
	node = pydot.Node(addr)
	nodos[addr] = node		
	graph.add_node(node)

	return node
	
	
def getNode(addr):
	if addr in nodos.keys():
		node = nodos[addr]
	else:
		node = newNode(addr)
	return node
	
# Parseo y construccion de grafico de registros ARP exportados a txt desde Wireshark
for l in f:
	count += 1	
	if (count == maxCount) :
		break;
	
	gratuitous_first_index = l.find("Gratuitous ARP for ")	
	if gratuitous_first_index != -1 :
		gratuitous_last_index = l.find("(Request)")
		from_addr = to_addr = l[gratuitous_first_index + 19 : gratuitous_last_index - 1]								 
			
	else :
		tell_index = l.find("Tell ")		
		if tell_index != -1 :
			from_addr = l[tell_index + 5 : len(l) - 1]
			who_has_index = l.find("Who has ")
			q_mark_index = l.find("? ")
			to_addr = l[who_has_index + 8 : q_mark_index]								 									
		else :
			continue 
			
	from_node = getNode(from_addr)
	to_node = getNode(to_addr)
	
	edge = from_addr + ":" + to_addr
	if not edge in agregados:
		graph.add_edge(pydot.Edge(from_node, to_node))
		agregados.add(edge)

graph.write_png('/<WORKSPACE>/tp1/informe/img/graph.png')

